package com.hua.yuan.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hua.yuan.resource.R;

/**
 * 创建时间:2017/2/8
 * 编写者：蓝天
 * 功能描述：多选择列表pop
 * 备注：支持多选列表pop，目前支持点击立即回调，如需要在窗口消失回调实现PopupWindow.OnDismissListener接口并在其中处理逻辑即可
 */

public class SortListPop implements PopupWindow.OnDismissListener{//implements PopupWindow.OnDismissListener

    private View contentView;
    private Context context;
    private PopupWindow popWindow;

    private LinearLayout ll_sort_time;
    private TextView tv_sort_time;
    private ImageView iv_gou_sort_time;

    private LinearLayout ll_sort_ascending;
    private TextView tv_sort_ascending;
    private ImageView iv_gou_sort_ascending;

    private LinearLayout ll_sort_descending;
    private TextView tv_sort_descending;
    private ImageView iv_gou_sort_descending;

    public PopupWindow create(Context context) {
        this.context = context;
        popWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        contentView = View.inflate(context, R.layout.pop_list_item, null);
        contentView.setFocusableInTouchMode(true);//收敛焦点
        popWindow.setContentView(contentView);
        popWindow.setFocusable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        backgroundAlpha(0.68f);
        popWindow.setOnDismissListener(this);

        initItem();

        return popWindow;
    }

    private void initItem() {
        ll_sort_time = contentView.findViewById(R.id.ll_sort_time);
        tv_sort_time = contentView.findViewById(R.id.tv_sort_time);
        iv_gou_sort_time = contentView.findViewById(R.id.iv_gou_sort_time);

        ll_sort_ascending = contentView.findViewById(R.id.ll_sort_ascending);
        tv_sort_ascending = contentView.findViewById(R.id.tv_sort_ascending);
        iv_gou_sort_ascending = contentView.findViewById(R.id.iv_gou_sort_ascending);

        ll_sort_descending = contentView.findViewById(R.id.ll_sort_descending);
        tv_sort_descending = contentView.findViewById(R.id.tv_sort_descending);
        iv_gou_sort_descending = contentView.findViewById(R.id.iv_gou_sort_descending);

        ll_sort_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTextColor(0);
            }
        });
        ll_sort_ascending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTextColor(1);
            }
        });
        ll_sort_descending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeTextColor(2);
            }
        });
    }

    private void changeTextColor(int pos) {
        initPosState(pos);

        if (null != mOnSelectClickListener) {
            mOnSelectClickListener.onSelectClick(pos);
        }
    }

    public void initPosState(int pos) {
        tv_sort_time.setTextColor(context.getResources().getColor(R.color.no_select));
        tv_sort_ascending.setTextColor(context.getResources().getColor(R.color.no_select));
        tv_sort_descending.setTextColor(context.getResources().getColor(R.color.no_select));
        iv_gou_sort_time.setVisibility(View.INVISIBLE);
        iv_gou_sort_ascending.setVisibility(View.INVISIBLE);
        iv_gou_sort_descending.setVisibility(View.INVISIBLE);
        if (pos == 0) {
            iv_gou_sort_time.setVisibility(View.VISIBLE);
            tv_sort_time.setTextColor(context.getResources().getColor(R.color.red_select));
        } else if (pos == 1) {
            iv_gou_sort_ascending.setVisibility(View.VISIBLE);
            tv_sort_ascending.setTextColor(context.getResources().getColor(R.color.red_select));
        } else if (pos == 2) {
            iv_gou_sort_descending.setVisibility(View.VISIBLE);
            tv_sort_descending.setTextColor(context.getResources().getColor(R.color.red_select));
        }
    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1f);
    }

    public interface OnSelectClickListener {
        void onSelectClick(int position);
    }

    private static OnSelectClickListener mOnSelectClickListener;

    public static void setOnSelectClickListener(OnSelectClickListener _onSelectClickListener) {
        mOnSelectClickListener = _onSelectClickListener;
    }

    protected <T> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }

    protected void showToast(String msg) {
        if (null != context) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }

    private static SortListPop cardOperatePop;

    private SortListPop() {
    }

    //唯一实例入口
    public static SortListPop getInstance() {
        if (null == cardOperatePop) {
            synchronized (SortListPop.class) {
                if (null == cardOperatePop) {
                    cardOperatePop = new SortListPop();
                }
            }
        }
        return cardOperatePop;
    }
}
